package itsm.isperp.module.controller.performance;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseProcessController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.performance.PerPerformance;
import itsm.isperp.module.entity.performance.PerPerformanceSetting;
import itsm.isperp.module.service.app.AppRoleService;
import itsm.isperp.module.service.performance.PerPerformanceService;
import itsm.isperp.module.service.performance.PerPerformanceSettingService;
import itsm.isperp.module.service.performance.PerPerformanceSettingUsersService;
import itsm.isperp.workflow.utils.BpmConst;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2017-08-29 09:15
 */
@Controller
@RequestMapping("performance/performance")
public class PerPerformanceController extends
    BaseProcessController<PerPerformance> {

  @Autowired
  protected PerPerformanceService perPerformanceService;

  @Override
  public PerPerformanceService getService() {
    return this.perPerformanceService;
  }

  @Override
  public String getPageTitle() {
    return "绩效考核";
  }

  @Override
  public String getProceessKey() {
    // TODO Auto-generated method stub
    return "performanceProcess";
  }

  @RequestMapping("edit")
  @Override
  public ModelAndView edit(String id, HttpServletRequest req) throws Exception {
    ModelAndView mv = this.getModelAndView();
    PerPerformance model = null;
    if (StringUtils.isEmpty(id)) {
      model = new PerPerformance();
      model.setId(HibernateUUIDGenerator.generate());
      model.setSelfSubmitDate(new Date());
      IUser us = ContextHolder.getLoginUser();
      String curUserFullname = us.getFullname();
      String curUserName = us.getUsername();
      model.setPerUserFullname(curUserFullname);
      model.setPerUserName(curUserName);

      PerPerformanceSettingUsersService perPerformanceSettingUsersService = ContextHolder
          .getSpringBean("perPerformanceSettingUsersService");
      String positionName = perPerformanceSettingUsersService
          .getUserPosition(curUserName);
      model.setPositionName(positionName);

    } else {
      model = this.getService().findOne(id);
    }
    String username=ContextHolder.getLoginUsername();
    PerPerformanceSettingService perPerformanceSettingService = ContextHolder
        .getSpringBean("perPerformanceSettingService");
    JqGridRequest request=new JqGridRequest();
    request.setSearchString(" (creator='"+username+"' OR 'admin'='"+username+"' OR (EXISTS(SELECT 1 FROM per_performance_setting_users WHERE setting_id=id AND app_user_name='"+username+"')))");
    List<PerPerformanceSetting> persetList=perPerformanceSettingService.findAll(request).getContent();
    mv.addObject("persetList", persetList);
    mv.addObject("model", model);
    return mv;
  }
  
  @RequestMapping("step1Edit")
  public ModelAndView step1Edit(String id, HttpServletRequest req) throws Exception {
    ModelAndView mv = this.getModelAndView();
    PerPerformance model = null;
    if (StringUtils.isEmpty(id)) {
      model = new PerPerformance();
      model.setId(HibernateUUIDGenerator.generate());
      model.setSelfSubmitDate(new Date());
      IUser us = ContextHolder.getLoginUser();
      String curUserFullname = us.getFullname();
      String curUserName = us.getUsername();
      model.setPerUserFullname(curUserFullname);
      model.setPerUserName(curUserName);

      PerPerformanceSettingUsersService perPerformanceSettingUsersService = ContextHolder
          .getSpringBean("perPerformanceSettingUsersService");
      String positionName = perPerformanceSettingUsersService
          .getUserPosition(curUserName);
      model.setPositionName(positionName);

    } else {
      model = this.getService().findOne(id);
    }
    String username=ContextHolder.getLoginUsername();
    PerPerformanceSettingService perPerformanceSettingService = ContextHolder
        .getSpringBean("perPerformanceSettingService");
    JqGridRequest request=new JqGridRequest();
    request.setSearchString(" (creator='"+username+"' OR 'admin'='"+username+"' OR (EXISTS(SELECT 1 FROM per_performance_setting_users WHERE setting_id=id AND app_user_name='"+username+"')))");
    List<PerPerformanceSetting> persetList=perPerformanceSettingService.findAll(request).getContent();
    mv.addObject("persetList", persetList);
    mv.addObject("model", model);
    return mv;
  }
  
  @RequestMapping("step2Edit")
  public ModelAndView step2Edit(String id, HttpServletRequest req) throws Exception {
    ModelAndView mv = this.getModelAndView();
    PerPerformance model = null;
    if (StringUtils.isEmpty(id)) {
      model = new PerPerformance();
      model.setId(HibernateUUIDGenerator.generate());
      model.setSelfSubmitDate(new Date());
      IUser us = ContextHolder.getLoginUser();
      String curUserFullname = us.getFullname();
      String curUserName = us.getUsername();
      model.setPerUserFullname(curUserFullname);
      model.setPerUserName(curUserName);

      PerPerformanceSettingUsersService perPerformanceSettingUsersService = ContextHolder
          .getSpringBean("perPerformanceSettingUsersService");
      String positionName = perPerformanceSettingUsersService
          .getUserPosition(curUserName);
      model.setPositionName(positionName);

    } else {
      model = this.getService().findOne(id);
    }
    mv.addObject("model", model);
    return mv;
  }

  @RequestMapping("approve1Edit")
  public ModelAndView approve1Edit(String id, HttpServletRequest req) {
    ModelAndView mv = new ModelAndView();
    PerPerformance model = this.getService().findOne(id);
    mv.addObject("model", model);
    return mv;

  }
  
  @Override
  @RequestMapping(value = { "manage",""})
  public ModelAndView manage(HttpServletRequest req) {
    ModelAndView mv = new ModelAndView("performance/performance/manage");
    String curUser=ContextHolder.getLoginUsername();
    String searchStr = "";
    searchStr = " (('admin'='"+curUser+"') OR ("
        + BpmConst.getQueryMyAllRelateTaskSqlCondition("%",
            curUser) + "))";
    String sql="select per_user_fullname from per_performance where "+searchStr+" group by per_user_fullname";
    JdbcTemplate jdbcTemplate = ContextHolder
        .getSpringBean("jdbcTemplate");
    List<Map<String,Object>>  userList=jdbcTemplate.queryForList(sql);
    mv.addObject("userList",userList);
    return mv;
  }

  @RequestMapping("searchManage")
  public ModelAndView searchManage(String id, HttpServletRequest req) {
    ModelAndView mv = new ModelAndView();
    String curUser=ContextHolder.getLoginUsername();
    String searchStr = "";
    AppRoleService appRoleService = ContextHolder.getSpringBean("appRoleService");
    String roleId = appRoleService.getRoleIdByName("管理员", curUser);
    String filter="1=2";
    if(StringUtils.isNotEmpty(roleId))
      filter="1=1";
    searchStr = " (("+filter+") OR ("
        + BpmConst.getQueryMyAllRelateTaskSqlCondition("%",
            curUser) + "))";
    String sql="select per_user_fullname from per_performance where "+searchStr+" group by per_user_fullname";
    JdbcTemplate jdbcTemplate = ContextHolder
        .getSpringBean("jdbcTemplate");
    List<Map<String,Object>>  userList=jdbcTemplate.queryForList(sql);
    mv.addObject("userList",userList);
    return mv;

  }

  @RequestMapping("view")
  public ModelAndView view(String id, HttpServletRequest req) {
    ModelAndView mv = new ModelAndView();
    PerPerformance model = this.getService().findOne(id);
    mv.addObject("model", model);
    return mv;

  }

  @RequestMapping("listSearchData")
  @ResponseBody
  public PageResponse<Map<String, Object>> listSearchData(
      JqGridRequest request, HttpServletRequest req) {
    String curUser=ContextHolder.getLoginUsername();
    String searchStr = "";
    AppRoleService appRoleService = ContextHolder.getSpringBean("appRoleService");
    String roleId = appRoleService.getRoleIdByName("管理员", curUser);
    String filter="1=2";
    if(StringUtils.isNotEmpty(roleId))
      filter="1=1";
    searchStr = " (("+filter+") OR ("
        + BpmConst.getQueryMyAllRelateTaskSqlCondition("%",
            curUser) + "))";

    request.setSearchString(searchStr);
    return this.getService().findAllDetailByRequest(request).toPage();

  }

}
